package cn.dy.workatten.imp;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.dy.base.framework.esb.def.RepMessage;
import cn.dy.workatten.dao.ScheduleCfgDao;
import cn.dy.workatten.pojo.ScheduleCfg;
import cn.dy.workatten.service.ScheduleCfgService;

public class ScheduleCfgServiceImpl implements ScheduleCfgService {

	protected static Logger logger = LoggerFactory.getLogger(ScheduleCfgServiceImpl.class);
	private ScheduleCfgDao scheduleCfgDao;

	public ScheduleCfgServiceImpl() {
		this.scheduleCfgDao = new ScheduleCfgDao();
	}

	@Override
	public RepMessage deleteScheduleCfgById(long id) {
		RepMessage repMessage = new RepMessage();
		ScheduleCfg scheduleCfg = this.scheduleCfgDao.getScheduleCfgById(id);
		String name = scheduleCfg.getSchedule_name();

		// addOperateLogRequest("删除班次信息", "删除班次信息为" + name + "的信息");
		try {
			this.scheduleCfgDao.deleteScheduleCfgById(id);
			repMessage.setResponse_code("0");
			repMessage.setResponse_desc("删除班次信息成功");
		} catch (Exception e) {
			repMessage.setResponse_code("-1");
			repMessage.setResponse_desc("删除班次信息失败");
			logger.error("删除班次信息失败", e);
		}
		return repMessage;
	}

	@Override
	public RepMessage updateScheduleCfg(ScheduleCfg scheduleCfg) {
		RepMessage repMessage = new RepMessage();
		String name = scheduleCfg.getSchedule_name();
//		addOperateLogRequest("修改角色信息", "修改角色为" + name + "的信息");
		try {
			// 根据id获取对应的班次信息，然后只设置系数和工时
			ScheduleCfg tempInfo =this.scheduleCfgDao.getScheduleCfgById(scheduleCfg.getId());
			tempInfo.setMan_hour(scheduleCfg.getMan_hour()); //工时
			tempInfo.setRatio(scheduleCfg.getRatio()); // 系数
			tempInfo.setRemark(scheduleCfg.getRemark()); // 备注
			
			Map content = new HashMap();
			content.put("int", Integer.valueOf(this.scheduleCfgDao.updateScheduleCfg(tempInfo)));
			repMessage.setContent(content);
			repMessage.setResponse_code("0");
			repMessage.setResponse_desc("修改班次信息成功");
		} catch (Exception e) {
			repMessage.setResponse_code("-1");
			repMessage.setResponse_desc("修改班次信息失败");
			repMessage.setResponse_detail(e.getLocalizedMessage());
			logger.error("修改班次信息失败", e);
		}
		return repMessage;
	}

	@Override
	public RepMessage getAllScheduleCfg() {
		RepMessage repMessage = new RepMessage();
		try {
			Map<String, Object> content = new HashMap();
			content.put("result", this.scheduleCfgDao.getAllScheduleCfg());
			repMessage.setContent(content);
			repMessage.setResponse_code("0");
		} catch (Exception e) {
			repMessage.setResponse_code("-1");
			repMessage.setResponse_desc("获取班次信息列表失败");
			repMessage.setResponse_detail(e.getLocalizedMessage());
			logger.warn("获取班次信息列表失败");
		}
		return repMessage;
	}

	
	@Override
	public RepMessage getScheduleCfgById(long id) {
		RepMessage repMessage = new RepMessage();
		try {
			Map<String, Object> content = new HashMap();
			content.put("result", this.scheduleCfgDao.getScheduleCfgById(id));
			repMessage.setContent(content);
			repMessage.setResponse_code("0");
		} catch (Exception e) {
			repMessage.setResponse_code("-1");
			repMessage.setResponse_desc("获取班次信息详情失败");
			repMessage.setResponse_detail(e.getLocalizedMessage());
			logger.warn("获取班次信息详情失败");
		}
		return repMessage;
	}

	@Override
	public RepMessage queryScheduleCfgByPage(String schedule_type, String schedule_name, int pageNum, int pageSize) {
		RepMessage repMessage = new RepMessage();
		try {
			Map<String, Object> map = new HashMap();
			map.put("result",
					this.scheduleCfgDao.queryScheduleCfgByPage(schedule_type, schedule_name, pageNum, pageSize));
			repMessage.setContent(map);
			repMessage.setResponse_code("0");
		} catch (Exception e) {
			repMessage.setResponse_code("-1");
			repMessage.setResponse_desc("条件查询日志失败");
			repMessage.setResponse_detail(e.getLocalizedMessage());
			logger.warn("条件查询日志失败");
		}
		return repMessage;
	}

}
